//初始化
mui.init();
var mask = mui.createMask(); //遮罩层
//获取本地储存数据
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var is_realname, pay_password, description;
var status = 0; //修改手机号审核状态
//返回user页面
$(document).on('tap', '.go_user', function() {
	window.top.location.href = "user-upgrade.html";
})

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		//self = plus.webview.currentWebview();
		userinfo(); //用户接口方法
		plus.nativeUI.closeWaiting(); //关闭等待
		mask.close(); //关闭遮罩层
		//页面刷新
		window.addEventListener('refresh_method', function(e) { //执行刷新
			location.reload();
		});
	});
}


//userinfo接口调用
function userinfo() {
	mui.ajax(user_url + '/api/user/getuserInfo', {
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			console.log(JSON.stringify(data))
			if(data.code == 10000) {
				description = data.resp.description; //实名认证驳回原因
				$(".balance-num").text(data.resp.balance);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});

}

//我的团队
document.getElementById("sure-do").addEventListener("tap", function() {
	payPanel.showPanel(function() {
		payPanel.closePanel();
		judge_payment_password(); //确认支付（判断支付密码是否正确）接口
		//payPanel.isShow;该属性能够判断面板是否关闭，可用于点击返回键时判断如果面板没关闭就关闭面板，而不是返回上个页面
	});
});


//确认密码
function judge_payment_password(ids){
//	console.log(ids)
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
	//判断支付密码是否正确
	console.log('支付密码')
	console.log(pwds)
	
	var dataCash = {
		password:pwds,      //支付密码
	};
	console.log(JSON.stringify(dataCash))
	mui.ajax(integral_url + '/api/business/upToBusiness', {
		//查询指定积分账户信息接口
		data:dataCash,
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			console.log(JSON.stringify(data))
			payPanel.closePanel();
			if(data.code == 1000) {				
				window.top.location.href = '../my/user_success.html'
//				mui.openWindow({
//					url: '../my/user-upgrade.html',
//					createNew: true
//				});				
			}
			else{
				mui.toast(data.msg);
				if(data.msg=='支付密码未设置'){
					//密码是否为空
					mui.openWindow({
						url: "../my/payment_pwd.html",
						createNew: true
					});
				}
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});
}
 

/*==================================================
输入支付密码  开始
 ==================================================*/
(function() {
	window.payPanel = {
		/**
		 * 显示输入面板，指定输入完成后的回调事件和点击取消的回调时间
		 * @param {Object} _inputOverFn
		 * @param {Object} _cancelFn
		 */
		showPanel: function(_inputOverFn, _cancelFn) {
			//输入完成后的回调
			panelObj.inputOverFn = _inputOverFn;
			
			//取消后的回调
			panelObj.cancelFn = _cancelFn;

			document.getElementsByClassName("pays")[0].style.display = "block";
			this.isShow = true; 
		},
		/**
		 * 关闭输入面板
		 */
		closePanel: function() {
			document.getElementsByClassName("pays")[0].style.display = "none";
			panelObj.clearAll();
			this.isShow = false;
		},
		isShow:false
	}
	//输入面板的对象及其所有方法
	var panelObj = {
		//输入面板内容
		template: "<div class='payPanel'><div class='pass'><i class='mui-icon mui-icon-closeempty close'></i><h4>请输入支付密码</h4><p id='pay_price'></p><ul><li></li><li></li><li></li><li></li><li></li><li></li></ul></div><div class='inputPanel'><div><ul><li><a data-value='1'>1</a></li><li><a data-value='2'>2</a></li><li><a data-value='3'>3</a></li><li><a data-value='4'>4</a></li><li><a data-value='5'>5</a></li><li><a data-value='6'>6</a></li><li><a data-value='7'>7</a></li><li><a data-value='8'>8</a></li><li><a data-value='9'>9</a></li><li class='cancel'><a data-value='cancel'>取消</a></li><li><a data-value='0'>0</a></li><li><a data-value='delete'>删除</a></li></ul></div></div></div>",
		//初始化面板，将面板添加到页面
		init: function() {
			var div = document.createElement("div");
			div.className = "pays";
			div.innerHTML = this.template;
			document.body.appendChild(div);
			this.addEvent();
		},
		//输入的密码值
		inputVal: "",
		//添加键盘点击事件
		addEvent: function() {
			var inputLi = document.getElementsByClassName("inputPanel")[0].getElementsByTagName("a");
			for(var i = 0, len = inputLi.length; i < len; i++) {
				var ele = inputLi[i];
				ele.addEventListener("touchstart", function(e) {
					e.preventDefault();
					var value = this.getAttribute("data-value");
					if(value == "cancel") {
						//取消支付
						panelObj.clearAll();
						payPanel.closePanel();
						panelObj.cancelFn!=undefined?panelObj.cancelFn():"";

					} else if(value == "delete") {
						//删除
						var len = panelObj.inputVal.length;
						panelObj.inputVal = panelObj.inputVal.substr(0, len - 1);
						panelObj.clearInputContent(len - 1);

					} else {
						if(panelObj.inputVal.length >= 6) return;
						panelObj.inputVal += value;
						//设置输入框内容
						panelObj.setInputContent();
						if(panelObj.inputVal.length == 6) {
							//延迟2毫秒,保证用户可以看到输入了第六位
							setTimeout(function(){
								pwds = panelObj.inputVal;
								panelObj.inputOverFn!=undefined?panelObj.inputOverFn(panelObj.inputVal):"";
								panelObj.clearAll();
							},200)
							
						}

					}

				})
			}
		},
		//给6个输入款填充内容
		setInputContent: function() {
			var index = panelObj.inputVal.length - 1;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[index].className = " i-circle";
		},
		//给指定的输入款取消内容
		clearInputContent: function(_index) {
			if(_index<0)return;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[_index].className = "";
		},
		//清除所有输入款内容
		clearAll: function() {
			this.inputVal = "";
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			for(var i = 0, len = passLi.length; i < len; i++) {
				passLi[i].className = "";
			}
		}, 
	}
	panelObj.init();
	// 点击 (x), 关闭弹窗
	$(document).on("tap", ".close", function() {
		panelObj.clearAll();
		//payPanel.closePanel();//关闭弹窗
		panelObj.cancelFn!=undefined?panelObj.cancelFn():""; 
		document.getElementsByClassName("pays")[0].style.display = "none"; 
		this.isShow = false;
		
		$(".zhifu_centent").show(); //订单弹窗
		$(".zz").show(); //遮盖层 
		
	});
})(window)

/*==================================================
输入支付密码  结束
 ==================================================*/